{{!
  Copyright (c) HashiCorp, Inc.
  SPDX-License-Identifier: BUSL-1.1
~}}

<PageHeader as |p|>
  <p.top>
    <Hds::Breadcrumb>
      <Hds::Breadcrumb::Item
        @text={{this.backendPath}}
        @route="vault.cluster.secrets.backend"
        @model={{this.backendPath}}
        data-test-link="role-list"
      />
      <Hds::Breadcrumb::Item @text="Credentials" @route="vault.cluster.secrets.backend" @model={{this.backendPath}} />
      <Hds::Breadcrumb::Item @text={{this.roleName}} @route="vault.cluster.secrets.backend.show" @model={{this.roleName}} />
      <Hds::Breadcrumb::Item @text={{this.options.title}} @current={{true}} />
    </Hds::Breadcrumb>
  </p.top>
  <p.levelLeft>
    <h1 data-test-title class="title is-3">
      {{this.options.title}}
    </h1>
  </p.levelLeft>
</PageHeader>

{{#if this.model.hasGenerated}}
  <div class="box is-fullwidth is-sideless is-paddingless is-marginless">
    <MessageError @model={{this.model}} />
    {{#unless this.model.isError}}
      <Hds::Alert @type="inline" @color="warning" class="has-top-bottom-margin" data-test-warning as |A|>
        <A.Title>Warning</A.Title>
        <A.Description>
          You will not be able to access this information later, so please copy the information below.
        </A.Description>
      </Hds::Alert>
    {{/unless}}
    {{#each this.model.attrs as |attr|}}
      {{#if (eq attr.type "object")}}
        <InfoTableRow
          @label={{capitalize (or attr.options.label (humanize (dasherize attr.name)))}}
          @value={{stringify (get this.model attr.name)}}
        />
      {{else}}
        {{#if
          (or
            (eq attr.name "key")
            (eq attr.name "secretKey")
            (eq attr.name "securityToken")
            (eq attr.name "privateKey")
            attr.options.masked
          )
        }}
          {{#if (get this.model attr.name)}}
            <InfoTableRow
              @label={{capitalize (or attr.options.label (humanize (dasherize attr.name)))}}
              @value={{get this.model attr.name}}
            >
              <MaskedInput
                @value={{get this.model attr.name}}
                @name={{attr.name}}
                @displayOnly={{true}}
                @allowCopy={{true}}
              />
            </InfoTableRow>
          {{/if}}
        {{else if (and (get this.model attr.name) (or (eq attr.name "issueDate") (eq attr.name "expiryDate")))}}
          <InfoTableRow
            data-test-table-row
            @label={{capitalize (or attr.options.label (humanize (dasherize attr.name)))}}
            @value={{date-format (get this.model attr.name) "MMM dd, yyyy hh:mm:ss a"}}
          />
        {{else}}
          <InfoTableRow
            @label={{capitalize (or attr.options.label (humanize (dasherize attr.name)))}}
            @value={{get this.model attr.name}}
          />
        {{/if}}
      {{/if}}
    {{/each}}
  </div>
  <div class="field is-grouped box is-fullwidth is-bottomless">
    <div class="control">
      <Hds::Copy::Button @text="Copy credentials" @textToCopy={{this.model.toCreds}} class="primary" />
    </div>
    {{#if this.model.leaseId}}
      <div class="control">
        <Hds::Copy::Button @text="Copy Lease ID" @textToCopy={{this.model.leaseId}} class="secondary" />
      </div>
    {{/if}}
    <div class="control">
      {{#if this.options.backIsListLink}}
        <Hds::Button
          @text="Back"
          @color="secondary"
          @route="vault.cluster.secrets.backend.list-root"
          @model={{this.backendPath}}
          data-test-secret-generate-back={{true}}
        />
      {{else}}
        <Hds::Button
          @text="Back"
          @color="secondary"
          {{on "click" (action "newModel")}}
          data-test-secret-generate-back="true"
        />
      {{/if}}
    </div>
  </div>
{{else}}
  <form {{action "create" on="submit"}} data-test-secret-generate-form="true">
    <div class="box is-sideless no-padding-top is-fullwidth is-marginless">
      <NamespaceReminder @mode="generate" @noun="credential" />
      <MessageError @model={{this.model}} />
      {{#if this.model.helpText}}
        <p class="is-hint">{{this.model.helpText}}</p>
      {{/if}}
      {{#if this.model.fieldGroups}}
        <FormFieldGroupsLoop @model={{this.model}} @mode={{this.mode}} />
      {{else}}
        {{#each this.model.attrs as |attr|}}
          <FormField data-test-field={{true}} @attr={{attr}} @model={{this.model}} />
        {{/each}}
      {{/if}}
    </div>
    <div class="field is-grouped box is-fullwidth is-bottomless">
      <Hds::ButtonSet>
        <Hds::Button
          @text="Generate"
          @icon={{if this.loading "loading"}}
          type="submit"
          disabled={{this.loading}}
          data-test-secret-generate={{true}}
        />
        <Hds::Button
          @text="Cancel"
          @route="vault.cluster.secrets.backend.list-root"
          @model={{this.backendPath}}
          data-test-secret-generate-cancel={{true}}
        />
      </Hds::ButtonSet>
    </div>
  </form>
{{/if}}